package bdu;

import robocode.HitWallEvent;
import robocode.Robot;
import static robocode.util.Utils.normalRelativeAngleDegrees;
import java.awt.Color;
import robocode.ScannedRobotEvent;

/**
 * The robot tracks a target and moves in a circle to avoid fire Only fire when the target is stationary. 
 * Gun Tracking source: Kelli Sawai Sample robot Fire04.
 * 
 * @author Bao Huy Ung
 */
public class Scavenger extends Robot {

  // Keeps track of how long this robot has been searching for a target
  int count = 0;
  // How much to turn this robot's gun when searching
  double gunTurnAmt;

  /**
   * The robot tracks a target and moves in a circle to avoid fire Only fire when the target is
   * stationary Gun Tracking source: Kelli Sawai Sample robot Fire04.
   */
  public void run() {

    // set color of robot
    setBodyColor(Color.black);
    setGunColor(Color.green);
    setBulletColor(Color.red);

    // Keep the gun still when turning
    setAdjustGunForRobotTurn(true);

    // Initialize gunTurn to 10
    gunTurnAmt = 10;

    // search for enemy
    while (true) {

      // turn the Gun (looks for enemy)
      turnGunRight(gunTurnAmt);

      count++;
      // If target not seen for 2 turns, look left
      if (count > 2) {
        gunTurnAmt = -10;
      }
      // If target still not seen for 5 turns, look right
      if (count > 5) {
        gunTurnAmt = 10;
      }

    }
  }

  /**
   * Circle around enemy with tracked gun, fire only when enemy is stationary.
   * 
   * @param event The scanned robot event
   */
  public void onScannedRobot(ScannedRobotEvent event) {

    // This is our target. Reset count (see the run method)
    count = 0;

    gunTurnAmt =
        normalRelativeAngleDegrees(event.getBearing() + (getHeading() - getRadarHeading()));

    turnGunRight(gunTurnAmt);

    double turnAngle;

    // from testing:
    // wide turns avoid fire from larger range
    // shallow turns allow quick maneuver around a close target
    // set turn angle based on target distance
    if (event.getDistance() < 300) {
      turnAngle = 100;
    }
    else {
      turnAngle = 120;
    }

    turnRight(event.getBearing() + turnAngle);

    
    // fire at stationary targets with no movement or disabled targets
    if (event.getVelocity() == 0) {
      // firing formula based on max distance 1200 and target's distance
      fire(3.1 - event.getDistance() / 1200 * 3);
    }
    
    // check distance of nearby robot, if too close back up for maneuvering room
    if (event.getDistance() < 300) { 
      turnRight(gunTurnAmt);
      back(300 - event.getDistance());
    }
    else {
      back(100);
    }
    
    scan();
  }

  /**
   * on wall hit move back 100 pixels allows for self correction if tracking a moving target for
   * stationary targets allows minor fire evasion.
   * 
   * @param event robot colliding with wall
   **/
  public void onHitWall(HitWallEvent event) {

    turnRight(180);
    back(100);
    //move away from walls on collision to avoid being trapped
    turnRight(90);
    ahead(100);
    scan();
  }

}
